package com.feinno.module.security.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.feinno.module.security.domain.Resource;
import com.feinno.module.security.service.ResourceService;

public class NavigationTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 传入URL
	 */
	private String url;

	/**
	 * 结束标签
	 */
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return SKIP_BODY;
	}

	/**
	 * 开始标签
	 */
	public int doStartTag() throws JspException {
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext.getServletContext());
		ResourceService resourceService = (ResourceService) wac.getBean("resourceService");
		String s = "";
		JspWriter out = this.pageContext.getOut();
		try {
			Resource resource = resourceService.findByResString(url);
			if (resource != null) {
				s += resource.getMenu().getName();
				s += "&nbsp;<span>></span>&nbsp;";
				s += "<a href=\"" + resource.getResString() + "\"><font style='color: red;'>";
				s += resource.getName() + "</font></a>";
				out.print(s + "\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
